package igz.fleaxy.ui.preloader {
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * Clase que crea un preloader radial simple
	 * 
	 * @param	p_settings : Object = { "backLayer": displayObject_1 [Optional]
	 * 								  , "frontLayer": displayObject_2
	 * 								  , "textEnable": Boolean
	 * 								  , "textSize": uint
	 * 								  , "textColor": uint				
	 * 								  , "textDisplace": Point }
	 * 
	 * @example ScalePreloaderUi( {  "backLayer": displayObject_2
	 * 							   , "frontLayer": displayObject_2
	 * 							   , "textEnable": false
	 * 							   , "textSize": 15
	 * 							   , "textColor": 0xFFFFFF
	 * 							   , "textDisplace": new Point(5, -5);
	 * 							   }
	 * 							 );
	 */
	 
	public class RadialPreloaderUi extends Sprite {
		
		private const _TO_RADIANS : Number = ( Math.PI / 180 );
		
		private var _Radius     : Number = 25.0;
		private var _Thickness  : Number = 5.0;
		
		private var _BackLayer   : Sprite;
		private var _FrontLayer  : Sprite;
		private var _MaskLayer   : Shape;
		private var _CounterText : TextField; 
		
		private var _Settings : Object = { "backLayer": null 
										,  "frontLayer": null
										,  "textEnable": false
										,  "textSize":  12
										,  "textColor": 0xFFFFFF
										,  "textDisplace": new Point()
										};

		
		// Constructora
		public function RadialPreloaderUi( p_settings:Object = null ) {
			
			// Combinamos opciones
			ObjectUtil.$Merge( p_settings, _Settings );
			
			// Capa frontal
			_FrontLayer = new Sprite();
			addChild( _FrontLayer );
			
			if ( _Settings["frontLayer"] == null ) {
				_Load_DefaultPreloader();
			}
			else {
				_Load_CustomPreloader();
			}
			
			// Máscara
			_MaskLayer = new Shape();
			addChild( _MaskLayer );
			
			_FrontLayer.mask = _MaskLayer;
		}
	
		
		// Método que establece un cargador por defecto
		private function _Load_DefaultPreloader() : void {
			
			// Capa trasera
			_BackLayer = new Sprite();
			_BackLayer.graphics.lineStyle( _Thickness, 0x000033, 1, false );
			_BackLayer.graphics.drawCircle( _Radius, _Radius, _Radius );
			_BackLayer.graphics.endFill();
			addChild( _BackLayer );
			
			// Capa frontal
			_FrontLayer.graphics.lineStyle( _Thickness, 0x2E9AFE, 1, false );
			_FrontLayer.graphics.drawCircle( _Radius, _Radius, _Radius );
			_FrontLayer.graphics.endFill();
			
			// Cambio de orden entre capas
			swapChildren( _BackLayer, _FrontLayer );
		
			// Contador de progresion
			//_Create_CounterText();
		}
		
		
		// Método que establece un cargador personalizado
		private function _Load_CustomPreloader() : void {
			
			// Capa trasera
			if ( _Settings["backLayer"] != null ) {
				_BackLayer = new Sprite();
				_BackLayer.addChild( _Settings["backLayer"] );
				addChild( _BackLayer );
				swapChildren( _BackLayer, _FrontLayer );
			}
			
			// Capa frontal
			_FrontLayer.addChild( _Settings["frontLayer"] );
			
			// Calculo del radio en función del tamaño de la capa
			_Radius = _FrontLayer.width * 0.5;
			
			// Contador de completado
			if ( _Settings["textEnable"] ) {
				_Create_CounterText();
			}
		}
		
		
		// Método que establece un contador prar el cargador
		private function _Create_CounterText() : void {
			_CounterText = new TextField();
			_CounterText.defaultTextFormat = new TextFormat( "Verdana", _Settings["textSize"],_Settings["textColor"] );
			//_CounterText.textColor = _Settings["textColor"];
			_CounterText.selectable = _CounterText.multiline = false;
			addChild( _CounterText );
		}
		
		/**
		 * cambia el valor mostrador del preloader ( el porcentaje)
		 * @param	p_value porcentaje de carga
		 */
		public function $SetValue( p_value:uint ) : void {
			_UpdatePreloader( p_value );
		}
		
		private function _UpdatePreloader( p_value:uint ) : void {
			
			var t:Number;
			var x:Number;
			var y:Number;
		
			_MaskLayer.graphics.clear();
			_MaskLayer.graphics.beginFill( 0xFFFFFF, 0);
			_MaskLayer.graphics.moveTo( _Radius, _Radius );
			
			for ( t = _TO_RADIANS * ( -180 ); t <= _TO_RADIANS * ( -180 + ( p_value * 361 ) / 100); t += 0.1 ) {
				x = Number( _Radius - _Radius*3/2 * Math.sin( t ) );
				y = Number( _Radius + _Radius*3/2 * Math.cos( t ) );
				_MaskLayer.graphics.lineTo( x, y );
			}
			
			_MaskLayer.graphics.endFill();
			
			if ( _Settings["textEnable"] ) {
				_UpdateCounterText( p_value );
			}
		}
		
		private function _UpdateCounterText( p_value:uint ) : void {
			_CounterText.htmlText = String( p_value + " % ");
			_CounterText.x = _Radius - _CounterText.textWidth * 0.5 + _Settings["textDisplace"].x;
			_CounterText.y = _Radius - _CounterText.textHeight * 0.5 + _Settings["textDisplace"].y;
		}		
	}

}